home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KoRuler.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  11.2 KB  |  373 lines

  1. /* This file is part of the KDE project
  2.    Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
  3.  
  4.    This library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2 of the License, or (at your option) any later version.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.  * Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. // Description: Ruler (header)
  21.  
  22. /******************************************************************/
  23.  
  24. #ifndef koRuler_h
  25. #define koRuler_h
  26.  
  27. #include <qframe.h>
  28. #include <qpixmap.h>
  29.  
  30. #include <kdemacros.h>
  31. #include <koffice_export.h>
  32. #include <KoGlobal.h>
  33. #include <KoTabChooser.h>
  34. #include <KoUnit.h>
  35.  
  36. class KoPageLayout;
  37. class QPainter;
  38.  
  39. enum KoTabulators { T_LEFT = 0, T_CENTER = 1, T_RIGHT = 2, T_DEC_PNT = 3, T_INVALID = -1 };
  40. enum KoTabulatorFilling { TF_BLANK = 0, TF_DOTS = 1, TF_LINE = 2, TF_DASH = 3, TF_DASH_DOT = 4, TF_DASH_DOT_DOT = 5};
  41.  
  42. /**
  43.  * Struct: KoTabulator
  44.  * Defines the position of a tabulation (in pt), and its type
  45.  */
  46. struct KoTabulator {
  47.     /**
  48.      * Position of the tab in pt
  49.      */
  50.     double ptPos;
  51.     /**
  52.      * Type of tab (left/center/right/decimalpoint)
  53.      */
  54.     KoTabulators type;
  55.     /**
  56.      * Type of tab filling.
  57.      */
  58.     KoTabulatorFilling filling;
  59.     /**
  60.      * Width of the tab filling line.
  61.      */
  62.     double ptWidth;
  63.     /**
  64.      * Alignment character.
  65.      */
  66.     QChar alignChar;
  67.  
  68.     bool operator==( const KoTabulator & t ) const {
  69.         return QABS( ptPos - t.ptPos ) < 1E-4 && type == t.type &&
  70.                filling == t.filling && QABS( ptWidth - t.ptWidth ) < 1E-4;
  71.     }
  72.     bool operator!=( const KoTabulator & t ) const {
  73.         return !operator==(t);
  74.     }
  75.     // Operators used for sorting
  76.     bool operator < ( const KoTabulator & t ) const {
  77.         return ptPos < t.ptPos;
  78.     }
  79.     bool operator <= ( const KoTabulator & t ) const {
  80.         return ptPos <= t.ptPos;
  81.     }
  82.     bool operator > ( const KoTabulator & t ) const {
  83.         return ptPos > t.ptPos;
  84.     }
  85. };
  86.  
  87. typedef QValueList<KoTabulator> KoTabulatorList;
  88.  
  89. class KoRulerPrivate;
  90.  
  91. /**
  92.  * KoRuler is the horizontal or vertical ruler, to be used around
  93.  * the drawing area of most KOffice programs.
  94.  *
  95.  * It shows the graduated ruler with numbering, in any of the base units (like mm/pt/inch),
  96.  * and supports zooming, tabulators, paragraph indents, showing the mouse position, etc.
  97.  *
  98.  * It also offers a popupmenu upon right-clicking, for changing the unit,
  99.  * the page layout, or removing a tab.
  100.  */
  101. class KOFFICEUI_EXPORT KoRuler : public QFrame
  102. {
  103.     Q_OBJECT
  104.     friend class KoRulerPrivate; // for the Action enum
  105. public:
  106.     static const int F_TABS;
  107.     static const int F_INDENTS;
  108.     static const int F_HELPLINES;
  109.     static const int F_NORESIZE;
  110.  
  111.     /**
  112.      * Create a ruler
  113.      * TODO document params
  114.      */
  115.     KoRuler( QWidget *_parent,  QWidget *_canvas, Orientation _orientation,
  116.              const KoPageLayout& _layout, int _flags, KoUnit::Unit _unit,
  117.              KoTabChooser *_tabChooser = 0L );
  118.     ~KoRuler();
  119.  
  120.     /**
  121.      * Set the unit to be used. The unit is specified using text as defined in KoUnit, for
  122.      * example "mm", "pt" or "inch".
  123.      * @deprecated You should use the KoUnit::Unit variant instead.
  124.      */
  125.     void setUnit( const QString& unit ) KDE_DEPRECATED ;
  126.     /**
  127.      * Set the unit to be used.
  128.      */
  129.     void setUnit( KoUnit::Unit unit );
  130.  
  131.     /**
  132.      * Set the zoom of the ruler (default value of 1.0 is 100%)
  133.      */
  134.     void setZoom( const double& zoom=1.0 );
  135.     /**
  136.      * @return the current zoom level
  137.      */
  138.     const double& zoom() const { return m_zoom; }
  139.  
  140.     /**
  141.      * Set the page layout, see @ref KoPageLayout.
  142.      * This defines the size of the page and the margins,
  143.      * from which the size of the ruler is deducted.
  144.      */
  145.     void setPageLayout( const KoPageLayout& _layout );
  146.  
  147.     /**
  148.      * Call showMousePos(true) if the ruler should indicate the position
  149.      * of the mouse. This is usually only the case for drawing applications,
  150.      * so it is not enabled by default.
  151.      */
  152.     void showMousePos( bool _showMPos );
  153.     /**
  154.      * Set the position of the mouse, to update the indication in the ruler.
  155.      * This is only effective if showMousePos(true) was called previously.
  156.      * The position to give is not zoomed, it's in real pixel coordinates!
  157.      */
  158.     void setMousePos( int mx, int my );
  159.  
  160.     /**
  161.      * Set a global offset to the X and Y coordinates.
  162.      * Usually the main drawing area is a QScrollView, and this is called
  163.      * with contentsX() and contentsY(), each time those values change.
  164.      */
  165.     void setOffset( int _diffx, int _diffy );
  166.  
  167.     /**
  168.      * Set the [paragraph] left indent to the specified position (in the current unit)
  169.      */
  170.     void setLeftIndent( double _left )
  171.     { i_left = makeIntern( _left ); update(); }
  172.  
  173.     /**
  174.      * Set the [paragraph] first-line left indent to the specified position (in the current unit)
  175.      * This indent is cumulated with the left or right margin, depending on the [paragraph] direction.
  176.      */
  177.     void setFirstIndent( double _first )
  178.     { i_first = makeIntern( _first ); update(); }
  179.  
  180.     /**
  181.      * Set the [paragraph] right indent to the specified position (in the current unit)
  182.      */
  183.     void setRightIndent( double _right );
  184.  
  185.     /**
  186.      * Set the [paragraph] direction. By default (rtl=false), the left indent is on the
  187.      * left, and the right indent is on the right ;)
  188.      * If rtl=true, it's the opposite.
  189.      */
  190.     void setDirection( bool rtl );
  191.  
  192.     /**
  193.      * Set the list of tabulators to show in the ruler.
  194.      */
  195.     void setTabList( const KoTabulatorList & tabList );
  196.  
  197.     /**
  198.      * Set the start and the end of the current 'frame', i.e. the part
  199.      * of the page in which we are currently working. See KWord frames
  200.      * for an example where this is used. The tab positions and paragraph
  201.      * indents then become relative to the beginning of the frame, and the
  202.      * ruler goes from frameStart to frameEnd instead of using the page margins.
  203.      * @p _frameStart et @p _frameEnd are in pixel coordinates.
  204.      */
  205.     void setFrameStartEnd( int _frameStart, int _frameEnd );
  206.  
  207.     /**
  208.      * KoRuler is in "read write" mode by default.
  209.      * Use setReadWrite(false) to use it in read-only mode.
  210.      */
  211.     void setReadWrite( bool _readWrite );
  212.  
  213.     /**
  214.      * Change the flag (i.e. activate or deactivate certain features of KoRuler)
  215.      */
  216.     void changeFlags(int _flags);
  217.  
  218.     /**
  219.      * Set the size of the grid used for tabs positioning, size in pt.
  220.      * default value is 0. 0 means no grid.
  221.      */
  222.     void setGridSize(double newGridSize) { gridSize=newGridSize; }
  223.  
  224.     /**
  225.      * @return the current flags
  226.      */
  227.     int flags() const;
  228.  
  229.     /**
  230.      * @return whether the current doubleClicked() signal was triggered
  231.      * by the user double-clicking on an indent (BCI).  It returns false
  232.      * if the user just double-clicked on an "empty" part of the ruler.
  233.      *
  234.      * This method is strictly provided for use in a slot connected to the
  235.      * doubleClicked() signal; calling it at any other time results in
  236.      * undefined behavior.
  237.      */
  238.     bool doubleClickedIndent() const;
  239.  
  240.     /**
  241.      * Enable or disable the "Page Layout" menu item.
  242.      */
  243.     void setPageLayoutMenuItemEnabled(bool b);
  244.  
  245.     /**
  246.      * Reimplemented from QWidget
  247.      */
  248.     virtual QSize minimumSizeHint() const;
  249.  
  250.     /**
  251.      * Reimplemented from QWidget
  252.      */
  253.     virtual QSize sizeHint() const;
  254.  
  255. signals:
  256.     void newPageLayout( const KoPageLayout & );
  257.     void newLeftIndent( double );
  258.     void newFirstIndent( double );
  259.     void newRightIndent( double );
  260.     /** Old signal, kept for compatibility. Use doubleClicked instead. */
  261.     void openPageLayoutDia();
  262.     /** This signal is emitted when double-clicking the ruler (or an indent) */
  263.     void doubleClicked();
  264.     /** This signal is emitted when double-clicking a tab */
  265.     void doubleClicked( double ptPos );
  266.  
  267.     void tabListChanged( const KoTabulatorList & );
  268.     void unitChanged( KoUnit::Unit );
  269.  
  270.     void addGuide(const QPoint &, bool, int );
  271.     void moveGuide( const QPoint &, bool, int );
  272.     void addHelpline(const QPoint &, bool );
  273.     void moveHelpLines( const QPoint &, bool );
  274.  
  275. protected:
  276.     enum Action {A_NONE, A_BR_LEFT, A_BR_RIGHT, A_BR_TOP, A_BR_BOTTOM,
  277.                  A_LEFT_INDENT, A_FIRST_INDENT, A_TAB, A_RIGHT_INDENT,
  278.                  A_HELPLINES };
  279.  
  280.     void drawContents( QPainter *_painter )
  281.     { orientation == Qt::Horizontal ? drawHorizontal( _painter ) : drawVertical( _painter ); }
  282.  
  283.     void drawHorizontal( QPainter *_painter );
  284.     void drawVertical( QPainter *_painter );
  285.     void drawTabs( QPainter &_painter );
  286.  
  287.     void mousePressEvent( QMouseEvent *e );
  288.     void mouseReleaseEvent( QMouseEvent *e );
  289.     void mouseMoveEvent( QMouseEvent *e );
  290.     void mouseDoubleClickEvent( QMouseEvent* );
  291.     void resizeEvent( QResizeEvent *e );
  292.     void handleDoubleClick();
  293.  
  294.     double makeIntern( double _v );
  295.     double zoomIt(const double &value) const;
  296.     int zoomIt(const int &value) const;
  297.     unsigned int zoomIt(const unsigned int &value) const;
  298.     double unZoomIt(const double &value) const;
  299.     int unZoomIt(const int &value) const;
  300.     unsigned int unZoomIt(const unsigned int &value) const;
  301.     void setupMenu();
  302.     void uncheckMenu();
  303.     void searchTab(int mx);
  304.     void drawLine(int oldX, int newX);
  305.  
  306. private:
  307.     double applyRtlAndZoom( double value ) const;
  308.     double unZoomItRtl( int pixValue ) const;
  309.     double lineDistance() const;
  310.     bool willRemoveTab( int y ) const;
  311.  
  312.     KoRulerPrivate *d;
  313.  
  314.     Qt::Orientation orientation;
  315.     int diffx, diffy;
  316.     double i_left, i_first;
  317.     QPixmap buffer;
  318.     double m_zoom, m_1_zoom;
  319.     KoUnit::Unit m_unit;
  320.     bool hasToDelete;
  321.     bool showMPos;
  322.     bool m_bFrameStartSet;
  323.     bool m_bReadWrite;
  324.     int mposX, mposY;
  325.     int frameStart;
  326.  
  327.     double gridSize;
  328.  
  329. protected slots:
  330.     void slotMenuActivated( int i );
  331.     void pageLayoutDia() { emit doubleClicked()/*openPageLayoutDia()*/; }
  332.     void rbRemoveTab();
  333.  
  334. };
  335.  
  336. inline double KoRuler::zoomIt(const double &value) const {
  337.     if (m_zoom==1.0)
  338.         return value;
  339.     return m_zoom*value;
  340. }
  341.  
  342. inline int KoRuler::zoomIt(const int &value) const {
  343.     if (m_zoom==1.0)
  344.         return value;
  345.     return qRound(m_zoom*value);
  346. }
  347.  
  348. inline unsigned int KoRuler::zoomIt(const unsigned int &value) const {
  349.     if (m_zoom==1.0)
  350.         return value;
  351.     return static_cast<unsigned int>(qRound(m_zoom*value));
  352. }
  353.  
  354. inline double KoRuler::unZoomIt(const double &value) const {
  355.     if(m_zoom==1.0)
  356.         return value;
  357.     return value*m_1_zoom;
  358. }
  359.  
  360. inline int KoRuler::unZoomIt(const int &value) const {
  361.     if(m_zoom==1.0)
  362.         return value;
  363.     return qRound(value*m_1_zoom);
  364. }
  365.  
  366. inline unsigned int KoRuler::unZoomIt(const unsigned int &value) const {
  367.     if(m_zoom==1.0)
  368.         return value;
  369.     return static_cast<unsigned int>(qRound(value*m_1_zoom));
  370. }
  371.  
  372. #endif
  373.